﻿using System;
using System.Collections.Generic;
using System.Linq;
using WmsService.BusinessLogic;
using WmsService.Derived;
using WmsService.Entity;
using WmsService.Exceptions;
using WmsService.Factory;
using WmsService.In;

namespace WmsService
{
    public partial class Service
    {
        #region 发票 付款申请单

// ReSharper disable MemberCanBeMadeStatic.Local
        private IInvoiceAndPayApplyBillLogic InvoiceAndPayApplyBillLogic
// ReSharper restore MemberCanBeMadeStatic.Local
        {
            get { return LogicFactoryCreator.CreateAbstractFactory().CreateInvoiceAndPayApplyBillLogic(); }
        }

        public InvoiceBill GetInvoiceBill(string invoiceNo)
        {
            return InvoiceAndPayApplyBillLogic.GetInvoiceBill(invoiceNo);
        }

        public void SaveInvoiceBill(InvoiceBill invoiceBill)
        {
            InvoiceAndPayApplyBillLogic.SaveInvoiceBill(invoiceBill);
        }

        public void SaveInvoiceBillList(List<InvoiceBill> invoiceBills)
        {
            InvoiceAndPayApplyBillLogic.SaveInvoiceBillList(invoiceBills);
        }

        public void DeleteInvoiceBill(string invoiceNo)
        {
            InvoiceAndPayApplyBillLogic.DeleteInvoiceBill(invoiceNo);
        }

        public List<InvoiceBill> GetInvoiceBillsByCondition(string condition)
        {
            return InvoiceAndPayApplyBillLogic.GetInvoiceBillsByCondition(condition);
        }

        public List<ExtendInvoiceBill> GetExtendInvoiceBillsByCondition(string condition)
        {
            return InvoiceAndPayApplyBillLogic.GetExtendInvoiceBillsByCondition(condition);
        }

        public List<ExtendInvoiceBillInStoreBill> GetExtendInvoiceBillInStoreBillsByCondition(string condition)
        {
            return InvoiceAndPayApplyBillLogic.GetExtendInvoiceBillInStoreBillsByCondition(condition);
        }

        public InvoiceBillInStoreBill GetInvoiceBillInStoreBill(string invoiceNo, string inStoreBillID)
        {
            if (invoiceNo == null) throw new ArgumentNullException("invoiceNo");
            if (inStoreBillID == null) throw new ArgumentNullException("inStoreBillID");
            return InvoiceAndPayApplyBillLogic.GetInvoiceBillInStoreBill(invoiceNo, inStoreBillID);
        }

        /// <summary>
        ///   将入库单从发票binding中去掉，也就是删除关系
        /// </summary>
        /// <param name = "invoiceNo">发票号码</param>
        /// <param name = "inStoreBillID">入库单号码</param>
        public void CancelInStoreBillFromInvoiceBill(string invoiceNo, string inStoreBillID)
        {
            InvoiceAndPayApplyBillLogic.CancelInStoreBillFromInvoiceBill(invoiceNo, inStoreBillID);
        }

        public void SaveInvoiceBillInStoreBill(InvoiceBillInStoreBill invoiceBillInStoreBill)
        {
            InvoiceAndPayApplyBillLogic.SaveInvoiceBillInStoreBill(invoiceBillInStoreBill);
        }

        public void AddInvoiceBillInStoreBill(List<InvoiceBillInStoreBill> invoiceBillInStoreBills)
        {
            InvoiceAndPayApplyBillLogic.AddInvoiceBillInStoreBill(invoiceBillInStoreBills);
        }

        public List<InvoiceBillInStoreBill> GetInvoiceBillInStoreBillsByCondition(string condition)
        {
            return InvoiceAndPayApplyBillLogic.GetInvoiceBillInStoreBillsByCondition(condition);
        }

        public PayApplyBillInvoiceWrap GetPayApplyBillInvoiceWrap(string billID)
        {
            return InvoiceAndPayApplyBillLogic.GetPayApplyBillInvoiceWrap(billID);
        }

        public void SavePayApplyBillInvoiceWrap(PayApplyBillInvoiceWrap payApplyBillInvoiceWrap)
        {
            InvoiceAndPayApplyBillLogic.SavePayApplyBillInvoiceWrap(payApplyBillInvoiceWrap);
        }

        public void AffirmPayApplyBillInvoice(string billID)
        {
            InvoiceAndPayApplyBillLogic.AffirmPayApplyBillInvoice(billID);
        }

        public void AttachInvoiceBills2PayApplyBill(string payApplyBillID, List<ExtendInvoiceBill> invoiceBills)
        {
            InvoiceAndPayApplyBillLogic.AttachInvoiceBills2PayApplyBill(payApplyBillID, invoiceBills);
        }

        public void DeleteInvoiceBillFromPayApplyBill(string payApplyBillID, string invoiceNo)
        {
            InvoiceAndPayApplyBillLogic.DeleteInvoiceBillFromPayApplyBill(payApplyBillID, invoiceNo);
        }

        public void DeleteInvoiceBillFromPayApplyBill(string payApplyBillID, InvoiceBill invoiceBill)
        {
            DeleteInvoiceBillFromPayApplyBill(payApplyBillID, invoiceBill.InvoiceNo);
        }

        #endregion

        #region View

        public List<VPreviewStoreGoods> GetVPreviewStoreGoodsListByCondition(string condition)
        {
            try
            {
                var ctx = ContextFactory.Create();
                return
                    ctx.ExecuteStoreQuery<VPreviewStoreGoods>("select * from VPreviewStoreGoods where 1=1 " + condition)
                        .ToList();
            }
            catch (Exception ex)
            {
                ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        #endregion

        #region GetSpecificationTypeList

        public List<string> GetSpecificationTypeList()
        {
            try
            {
                var ctx = ContextFactory.Create();
                return
                    ctx.ExecuteStoreQuery<String>(
                        "select distinct SpecificationType from  dbo.StoreGoods union select distinct SpecificationType from  dbo.InStoreBillDtl ")
                        .ToList();
            }
            catch (Exception ex)
            {
                ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        public List<string> GetSpecificationTypeList(string newGoodsCode)
        {
            try
            {
                var ctx = ContextFactory.Create();
                return
                    ctx.ExecuteStoreQuery<String>(
                        String.Format(
                            "select distinct SpecificationType from  dbo.StoreGoods where NewGoodsCode='{0}' union select distinct SpecificationType from  dbo.InStoreBillDtl where NewGoodsCode='{0}'",
                            newGoodsCode)).ToList();
            }
            catch (Exception ex)
            {
                ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        public List<SpecificationTypeItem> GetSpecificationTypeItemList()
        {
            try
            {
                var ctx = ContextFactory.Create();
                return
                    ctx.ExecuteStoreQuery<SpecificationTypeItem>(
                        "select distinct NewGoodsCode, SpecificationType from  dbo.StoreGoods  union select distinct  NewGoodsCode,SpecificationType from  dbo.InStoreBillDtl ")
                        .ToList();
            }
            catch (Exception ex)
            {
                ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        #endregion
    }
}